package com.tarena.vehicle.management.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.vehicle.common.ex.ServiceException;
import com.tarena.vehicle.common.pojo.vo.PageData;
import com.tarena.vehicle.common.util.PageInfoToPageDataConverter;
import com.tarena.vehicle.common.web.ServiceCode;
import com.tarena.vehicle.management.converter.VehicleInsuranceConverter;
import com.tarena.vehicle.management.mapper.VehicleInsuranceMapper;
import com.tarena.vehicle.management.pojo.entity.VehicleInspection;
import com.tarena.vehicle.management.pojo.entity.VehicleInsurance;
import com.tarena.vehicle.management.pojo.param.VehicleInsuranceAddNewParam;
import com.tarena.vehicle.management.pojo.param.VehicleInsuranceUpdateParam;
import com.tarena.vehicle.management.pojo.query.VehicleInsuranceQuery;
import com.tarena.vehicle.management.pojo.vo.VehicleInsuranceListItemVO;
import com.tarena.vehicle.management.pojo.vo.VehicleInsuranceStrandVO;
import com.tarena.vehicle.management.service.IVehicleInsuranceService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VehicleInsuranceServiceImpl implements IVehicleInsuranceService {

    @Autowired
    private VehicleInsuranceConverter vehicleInsuranceConverter;

    @Autowired
    private VehicleInsuranceMapper vehicleInsuranceMapper;

    @Override public void addNew(VehicleInsuranceAddNewParam param) {
        int count=vehicleInsuranceMapper.getCountByPolicyNum(param.getPolicyNum());
        if (count > 0) {
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, "当前保险信息已经录入");
        }
        VehicleInsurance vehicleInsurance = vehicleInsuranceConverter.param2po(param);
        int row=vehicleInsuranceMapper.insert(vehicleInsurance);
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_INSERT, "添加车辆保险信息失败，服务器忙，请稍后再尝试！");
        }

    }

    @Override public VehicleInsuranceStrandVO getVehicleInsuranceById(Integer id) {
        VehicleInsuranceStrandVO vo=vehicleInsuranceMapper.getVehicleInsuranceById(id);
        if (vo == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "该车辆保险信息不存在");
        }
        return vo;
    }

    @Override public void updateVehicleInsuranceUndueStatusById(Integer id) {
        VehicleInsuranceStrandVO vo=vehicleInsuranceMapper.getVehicleInsuranceById(id);
        if (vo == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "该车辆保险信息不存在");
        }
        int row=vehicleInsuranceMapper.updateVehicleInsuranceUndueStatusById(id);
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_INSERT, "修改车辆状态信息失败，服务器忙，请稍后再尝试！");
        }
    }

    @Override public void updateVehicleInsuranceExpireStatusById(Integer id) {
        VehicleInsuranceStrandVO vo=vehicleInsuranceMapper.getVehicleInsuranceById(id);
        if (vo == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "该车辆保险信息不存在");
        }
        int row=vehicleInsuranceMapper.updateVehicleInsuranceExpireStatusById(id);
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_INSERT, "修改车辆状态信息失败，服务器忙，请稍后再尝试！");
        }
    }

    @Override public void updateVehicleInsurance(VehicleInsuranceUpdateParam param) {
        VehicleInsuranceStrandVO vo=vehicleInsuranceMapper.getVehicleInsuranceById(param.getId());
        if (vo == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "该车辆保险信息不存在");
        }
        VehicleInsurance vehicleInsurance =vehicleInsuranceConverter.param2po(param);
        int row=vehicleInsuranceMapper.updateVehicleInsurance(vehicleInsurance);
        if (row < 1) {
            throw new ServiceException(ServiceCode.ERROR_INSERT, "修改车辆信息失败，服务器忙，请稍后再尝试！");
        }
    }

    @Override public PageData<VehicleInsuranceListItemVO> getVehicleInsuranceAll(Integer num, Integer size,
        VehicleInsuranceQuery query) {
        if (num != null && size != null)
            PageHelper.startPage(num, size);
        VehicleInsurance vehicleInsurance = vehicleInsuranceConverter.query2po(query);
        List<VehicleInsuranceListItemVO> list= vehicleInsuranceMapper.getVehicleInsuranceAll(vehicleInsurance);
        PageInfo<VehicleInsuranceListItemVO> info = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(info);
    }

    @Override public void deleteVehicleInsuranceById(Integer id) {
        VehicleInsuranceStrandVO vo=vehicleInsuranceMapper.getVehicleInsuranceById(id);
        if (vo == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "该车辆保险信息不存在");
        }
        int row=vehicleInsuranceMapper.deleteVehicleInsuranceById(id);
        if (row < 1) {
            throw new ServiceException(ServiceCode.ERROR_INSERT, "删除车辆信息失败，服务器忙，请稍后再尝试！");
        }
    }
}
